package com.csun.cmny.provider.controller;


import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.OldInConstant;
import com.csun.cmny.provider.model.domain.OmcAppUserOldmanBind;
import com.csun.cmny.provider.model.vo.Oldmanvo;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OldmanService;
import com.csun.cmny.provider.service.OmcAppUserOldmanBindService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/aersonageAppController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcAersonageAppController extends BaseController {

    @Resource
    private OmcAppUserOldmanBindService appUserOldmanBindService;
    @Resource
    private OldmanService oldmanService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;


    //个人版app查询绑定的老人信息

    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/selectOldIdByApp")
    public Wrapper selectOldIdByApp(){

        OmcAppUserOldmanBind omcAppUserOldmanBind = new OmcAppUserOldmanBind();
        omcAppUserOldmanBind.setUserId(getLoginAuthDto().getUserId());

        List<OmcAppUserOldmanBind> select = appUserOldmanBindService.select(omcAppUserOldmanBind);

        if(PublicUtil.isEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("olaMan.user.bind.null", null));
        }

        List<Oldmanvo> oldMans = new ArrayList<>();
        for (OmcAppUserOldmanBind appUserOldmanBind : select) {
            Oldman oldman = new Oldman();
            oldman.setOldmanId(appUserOldmanBind.getOldmanId().intValue());

            Oldman old = oldmanService.selectByOld(oldman);
            if (PublicUtil.isNotEmpty(old)){
                if (old.getStatus()==OldInConstant.YIRUYUAN){
                    Oldmanvo oldmanvo = new Oldmanvo();

                    BeanUtils.copyProperties(old,oldmanvo);

                    oldmanvo.setGroupName((String) redisTemplate.opsForValue().get(RedisKeyUtil.getUacGroupName(oldmanvo.getHomeId().toString())));

                    oldMans.add(oldmanvo);
                }
            }
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),oldMans);
    }
}
